﻿using NModBusDrive;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Timers;
using System.Windows.Forms;
using WinformControls.Forms;

namespace TestModBus
{
    public partial class Form1 : FrmWithTitle, ILog, IDisposable
    {

        BussinessModel bussiness = new BussinessModel();
        public Form1()
        {
            InitializeComponent();
            Console.SetOut(new TextBoxWriter(tboxConsole));
            bussiness.serialPortUtil.CallDataReceived += SerialPortUtil_CallDataReceived;
            bussiness.Logger = this;
            this.FormClosing += Form1_FormClosing;
        }

        public bool IsStop { get; set; }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            bussiness.serialPortUtil.ClosePort();
        }


        private void SerialPortUtil_CallDataReceived(byte[] bits)
        {
            string str = PublicMethod.ByteToHexStr(bits);
            Console.WriteLine("收到回调数据-->{0}", str);
            if (str.IndexOf("FFFF") == 0) // 机械矩阵
            {
                if (bits[3] == 1)    // 动作完成
                {
                    Console.WriteLine("动作完成");
                    SetShePingBtEnable(true);
                    SetSingleSingeColor(bussiness.operTypeBt, ColorType.Success);
                    IsStop = true;
                }
                else if (bits[3] == 2) // X轴故障报警
                {
                    Console.WriteLine("X轴故障报警");
                    SetShePingBtEnable(true);
                    SetSingleSingeColor(bussiness.operTypeBt, ColorType.Error);
                    IsStop = true;
                }
                else if (bits[3] == 3) // Y轴故障报警
                {
                    Console.WriteLine("Y轴故障报警");
                    SetShePingBtEnable(true);
                    SetSingleSingeColor(bussiness.operTypeBt, ColorType.Error);
                    IsStop = true;
                }
                else if (bits[3] == 4) // Z轴故障报警
                {
                    Console.WriteLine("Z轴故障报警");
                    SetShePingBtEnable(true);
                    SetSingleSingeColor(bussiness.operTypeBt, ColorType.Error);
                    IsStop = true;
                }
                else if (bits[3] == 5) // 设备处于手动状态
                {
                    Console.WriteLine(string.Format("获取数据--->从站地址:{0} 开始位置:{1} 数据长度:{2} 执行状态:{3}", bussiness.StartJiXieAddressRead, bussiness.NumberJiXieOfPointsRead, "设备处于手动状态"));
                    SetShePingBtEnable(true);
                    SetSingleSingeColor(bussiness.operTypeBt, ColorType.Error);
                    IsStop = true;
                }
                else if (bits[3] == 6) // 设备处于急停状态
                {
                    Console.WriteLine(string.Format("获取数据--->从站地址:{0} 开始位置:{1} 数据长度:{2} 执行状态:{3}", bussiness.StartJiXieAddressRead, bussiness.NumberJiXieOfPointsRead, "设备处于急停状态"));
                    SetShePingBtEnable(true);
                    SetSingleSingeColor(bussiness.operTypeBt, ColorType.Error);
                    IsStop = true;
                }
                else if (bits[3] == 7) // 指令冲突
                {
                    Console.WriteLine(string.Format("获取数据--->从站地址:{0} 开始位置:{1} 数据长度:{2} 执行状态:{3}", bussiness.StartJiXieAddressRead, bussiness.NumberJiXieOfPointsRead, "指令冲突"));
                    SetShePingBtEnable(true);
                    SetSingleSingeColor(bussiness.operTypeBt, ColorType.Error);
                    IsStop = true;
                }
                else if (bits[3] == 8) // 运行中切换
                {
                    Console.WriteLine(string.Format("获取数据--->从站地址:{0} 开始位置:{1} 数据长度:{2} 执行状态:{3}", bussiness.StartJiXieAddressRead, bussiness.NumberJiXieOfPointsRead, "运行中切换"));
                    SetShePingBtEnable(true);
                    SetSingleSingeColor(bussiness.operTypeBt, ColorType.Error);
                    IsStop = true;
                }
            }
            else //FFEE 射频矩阵
            {
                if (bits[3] == 1)    // 动作完成
                {
                    SetShePingState(bussiness.operTypeBt, ColorType.Success);
                    SetShePingBtEnable(true);
                    Console.WriteLine("动作完成");
                    IsStop = true;
                }
                else if (bits[3] == 2) // 动作失败
                {
                    SetShePingState(bussiness.operTypeBt, ColorType.Error);
                    SetShePingBtEnable(true);
                    Console.WriteLine("动作失败");
                    IsStop = true;
                }
            }
        }

        /// <summary>
        /// 通过切换按钮 ---PLC
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChBt_Click(object sender, EventArgs e)
        {
            try
            {
                SetShePingBtEnable(false);
                SetCHDefaultState();
                Button button = (Button)sender;
                switch ((ModeOperType)Convert.ToInt32(button.Tag))
                {
                    case ModeOperType.CH1:
                        bussiness.operTypeBt = ModeOperType.CH1;
                        bussiness.now_function = "CH1";
                        break;
                    case ModeOperType.CH2:
                        bussiness.operTypeBt = ModeOperType.CH2;
                        bussiness.now_function = "CH2";
                        break;
                    case ModeOperType.CH3:
                        bussiness.operTypeBt = ModeOperType.CH3;
                        bussiness.now_function = "CH3";
                        break;
                    case ModeOperType.CH4:
                        bussiness.operTypeBt = ModeOperType.CH4;
                        bussiness.now_function = "CH4";
                        break;
                    case ModeOperType.CH5:
                        bussiness.operTypeBt = ModeOperType.CH5;
                        bussiness.now_function = "CH5";
                        break;
                    case ModeOperType.CH6:
                        bussiness.operTypeBt = ModeOperType.CH6;
                        bussiness.now_function = "CH6";
                        break;
                    case ModeOperType.CH7:
                        bussiness.operTypeBt = ModeOperType.CH7;
                        bussiness.now_function = "CH7";
                        break;
                    case ModeOperType.CH8:
                        bussiness.operTypeBt = ModeOperType.CH8;
                        bussiness.now_function = "CH8";
                        break;
                    case ModeOperType.CH9:
                        bussiness.operTypeBt = ModeOperType.CH9;
                        bussiness.now_function = "CH9";
                        break;
                    case ModeOperType.CH10:
                        bussiness.operTypeBt = ModeOperType.CH10;
                        bussiness.now_function = "CH10";
                        break;
                    case ModeOperType.CH11:
                        bussiness.operTypeBt = ModeOperType.CH11;
                        bussiness.now_function = "CH11";
                        break;
                    case ModeOperType.CH12:
                        bussiness.operTypeBt = ModeOperType.CH12;
                        bussiness.now_function = "CH12";
                        break;
                }
                bussiness.SendCh();
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("{0}\r\n{1}", ex.Message, ex.StackTrace));
            }

        }

        private void SetChBtEnable(bool isEnabed)
        {
            this.Invoke(new MethodInvoker(() =>
            {
                this.Ch1Bt.Enabled = isEnabed;
                this.Ch2Bt.Enabled = isEnabed;
                this.Ch3Bt.Enabled = isEnabed;
                this.Ch4Bt.Enabled = isEnabed;
                this.Ch5Bt.Enabled = isEnabed;
                this.Ch6Bt.Enabled = isEnabed;
                this.Ch7Bt.Enabled = isEnabed;
                this.Ch8Bt.Enabled = isEnabed;
                this.Ch9Bt.Enabled = isEnabed;
                this.Ch10Bt.Enabled = isEnabed;
                this.Ch11Bt.Enabled = isEnabed;
                this.Ch12Bt.Enabled = isEnabed;
            }));
        }

        private void SetSingleSingeColor(ModeOperType modeOper, ColorType color)
        {
            this.Invoke(new MethodInvoker(() =>
            {
                switch (modeOper)
                {
                    case ModeOperType.CH1:
                        this.Ch1.BackColor = SetRunColorState(color);
                        break;
                    case ModeOperType.CH2:
                        this.Ch2.BackColor = SetRunColorState(color);
                        break;
                    case ModeOperType.CH3:
                        this.Ch3.BackColor = SetRunColorState(color);
                        break;
                    case ModeOperType.CH4:
                        this.Ch4.BackColor = SetRunColorState(color);
                        break;
                    case ModeOperType.CH5:
                        this.Ch5.BackColor = SetRunColorState(color);
                        break;
                    case ModeOperType.CH6:
                        this.Ch6.BackColor = SetRunColorState(color);
                        break;
                    case ModeOperType.CH7:
                        this.Ch7.BackColor = SetRunColorState(color);
                        break;
                    case ModeOperType.CH8:
                        this.Ch8.BackColor = SetRunColorState(color);
                        break;
                    case ModeOperType.CH9:
                        this.Ch9.BackColor = SetRunColorState(color);
                        break;
                    case ModeOperType.CH10:
                        this.Ch10.BackColor = SetRunColorState(color);
                        break;
                    case ModeOperType.CH11:
                        this.Ch11.BackColor = SetRunColorState(color);
                        break;
                    case ModeOperType.CH12:
                        this.Ch12.BackColor = SetRunColorState(color);
                        break;
                }
            }));
        }

        private void BtClear_Click(object sender, EventArgs e)
        {
            this.tboxConsole.Clear();
        }

        #region 射频矩阵
        /// <summary>
        /// 射频矩阵按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ShePing_Click(object sender, EventArgs e)
        {
            try
            {
                SetShePingBtEnable(false);
                Button button = (Button)sender;
                bussiness.operTypeBt = (ModeOperType)Convert.ToInt32(button.Tag);
                switch (bussiness.operTypeBt)
                {
                    case ModeOperType.Y0:
                        if (bussiness.BtY0 == 0)
                        {
                            bussiness.RegisterBuffer[16] = 1;
                            bussiness.BtY0 = 1;
                        }
                        else
                        {
                            bussiness.BtY0 = 0;
                            bussiness.RegisterBuffer[16] = 0;
                        }
                        break;
                    case ModeOperType.Y1:
                        if (bussiness.BtY1 == 0)
                        {
                            bussiness.BtY1 = 1;
                            bussiness.RegisterBuffer[15] = 1;
                        }
                        else
                        {
                            bussiness.BtY1 = 0;
                            bussiness.RegisterBuffer[15] = 0;
                        }

                        break;
                    case ModeOperType.Y2:
                        if (bussiness.BtY2 == 0)
                        {
                            bussiness.BtY2 = 1;
                            bussiness.RegisterBuffer[14] = 1;
                        }
                        else
                        {
                            bussiness.BtY2 = 0;
                            bussiness.RegisterBuffer[14] = 0;
                        }
                        break;
                    case ModeOperType.Y3:
                        if (bussiness.BtY3 == 0)
                        {
                            bussiness.BtY3 = 1;
                            bussiness.RegisterBuffer[13] = 1;
                        }
                        else
                        {
                            bussiness.BtY3 = 0;
                            bussiness.RegisterBuffer[13] = 0;
                        }
                        break;
                    case ModeOperType.Y4:
                        if (bussiness.BtY4 == 0)
                        {
                            bussiness.BtY4 = 1;
                            bussiness.RegisterBuffer[12] = 1;
                        }
                        else
                        {
                            bussiness.BtY4 = 0;
                            bussiness.RegisterBuffer[12] = 0;
                        }
                        break;
                    case ModeOperType.Y5:
                        if (bussiness.BtY5 == 0)
                        {
                            bussiness.BtY5 = 1;
                            bussiness.RegisterBuffer[11] = 1;
                        }
                        else
                        {
                            bussiness.BtY5 = 0;
                            bussiness.RegisterBuffer[11] = 0;
                        }
                        break;
                    case ModeOperType.Y6:
                        if (bussiness.BtY6 == 0)
                        {
                            bussiness.BtY6 = 1;
                            bussiness.RegisterBuffer[10] = 1;
                        }
                        else
                        {
                            bussiness.BtY6 = 0;
                            bussiness.RegisterBuffer[10] = 0;
                        }
                        break;
                    case ModeOperType.Y7:
                        if (bussiness.BtY7 == 0)
                        {
                            bussiness.BtY7 = 1;
                            bussiness.RegisterBuffer[9] = 1;
                        }
                        else
                        {
                            bussiness.BtY7 = 0;
                            bussiness.RegisterBuffer[9] = 0;
                        }
                        break;
                    case ModeOperType.Y10:
                        if (bussiness.BtY10 == 0)
                        {
                            bussiness.BtY10 = 1;
                            bussiness.RegisterBuffer[8] = 1;
                        }
                        else
                        {
                            bussiness.BtY10 = 0;
                            bussiness.RegisterBuffer[8] = 0;
                        }
                        break;
                    case ModeOperType.Y11:
                        if (bussiness.BtY11 == 0)
                        {
                            bussiness.BtY11 = 1;
                            bussiness.RegisterBuffer[7] = 1;
                        }
                        else
                        {
                            bussiness.BtY11 = 0;
                            bussiness.RegisterBuffer[7] = 0;
                        }
                        break;
                    case ModeOperType.Y12:
                        if (bussiness.BtY12 == 0)
                        {
                            bussiness.BtY12 = 1;
                            bussiness.RegisterBuffer[6] = 1;
                        }
                        else
                        {
                            bussiness.BtY12 = 0;
                            bussiness.RegisterBuffer[6] = 0;
                        }
                        break;
                    case ModeOperType.Y13:
                        if (bussiness.BtY13 == 0)
                        {
                            bussiness.BtY13 = 1;
                            bussiness.RegisterBuffer[5] = 1;
                        }
                        else
                        {
                            bussiness.BtY13 = 0;
                            bussiness.RegisterBuffer[5] = 0;
                        }
                        break;
                    case ModeOperType.Y14:
                        if (bussiness.BtY14 == 0)
                        {
                            bussiness.BtY14 = 1;
                            bussiness.RegisterBuffer[4] = 1;
                        }
                        else
                        {
                            bussiness.BtY14 = 0;
                            bussiness.RegisterBuffer[4] = 0;
                        }
                        break;
                    case ModeOperType.Y15:
                        if (bussiness.BtY15 == 0)
                        {
                            bussiness.BtY15 = 1;
                            bussiness.RegisterBuffer[3] = 1;
                        }
                        else
                        {
                            bussiness.BtY15 = 0;
                            bussiness.RegisterBuffer[3] = 0;
                        }
                        break;
                    case ModeOperType.Y16:
                        if (bussiness.BtY16 == 0)
                        {
                            bussiness.BtY16 = 1;
                            bussiness.RegisterBuffer[2] = 1;
                        }
                        else
                        {
                            bussiness.BtY16 = 0;
                            bussiness.RegisterBuffer[2] = 0;
                        }
                        break;
                    case ModeOperType.Y17:
                        if (bussiness.BtY17 == 0)
                        {
                            bussiness.BtY17 = 1;
                            bussiness.RegisterBuffer[1] = 1;
                        }
                        else
                        {
                            bussiness.BtY17 = 0;
                            bussiness.RegisterBuffer[1] = 0;
                        }
                        break;
                    case ModeOperType.Y20:
                        if (bussiness.BtY20 == 0)
                        {
                            bussiness.BtY20 = 1;
                            bussiness.RegisterBuffer[0] = 1;
                        }
                        else
                        {
                            bussiness.BtY20 = 0;
                            bussiness.RegisterBuffer[0] = 0;
                        }
                        break;
                }
                bussiness.SendShePin();
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("{0}\r\n{1}", ex.Message, ex.StackTrace));
            }
        }

        /// <summary>
        /// 切换控件状态
        /// </summary>
        /// <param name="operType"></param>
        /// <param name="color"></param>
        private void SetShePingState(ModeOperType operType, ColorType color)
        {
            this.Invoke(new MethodInvoker(() =>
            {
                switch (operType)
                {
                    case ModeOperType.Y0:
                        if (bussiness.BtY0 != 0)
                        {
                            this.Y0.BackColor = SetRunColorState(color);
                        }
                        else
                        {
                            this.Y0.BackColor = SetRunColorState(ColorType.Default);
                        }
                        break;
                    case ModeOperType.Y1:
                        if (bussiness.BtY1 != 0)
                        {
                            this.Y1.BackColor = SetRunColorState(color);
                        }
                        else
                        {
                            this.Y1.BackColor = SetRunColorState(ColorType.Default);
                        }

                        break;
                    case ModeOperType.Y2:
                        if (bussiness.BtY2 != 0)
                        {
                            this.Y2.BackColor = SetRunColorState(color);
                        }
                        else
                        {
                            this.Y2.BackColor = SetRunColorState(ColorType.Default);
                        }
                        break;
                    case ModeOperType.Y3:
                        if (bussiness.BtY3 != 0)
                        {
                            this.Y3.BackColor = SetRunColorState(color);
                        }
                        else
                        {
                            this.Y3.BackColor = SetRunColorState(ColorType.Default);
                        }
                        break;
                    case ModeOperType.Y4:
                        if (bussiness.BtY4 != 0)
                        {
                            this.Y4.BackColor = SetRunColorState(color);
                        }
                        else
                        {
                            this.Y4.BackColor = SetRunColorState(ColorType.Default);
                        }
                        break;
                    case ModeOperType.Y5:
                        if (bussiness.BtY5 != 0)
                        {
                            this.Y5.BackColor = SetRunColorState(color);
                        }
                        else
                        {
                            this.Y5.BackColor = SetRunColorState(ColorType.Default);
                        }
                        break;
                    case ModeOperType.Y6:
                        if (bussiness.BtY6 != 0)
                        {
                            this.Y6.BackColor = SetRunColorState(color);
                        }
                        else
                        {
                            this.Y6.BackColor = SetRunColorState(ColorType.Default);
                        }
                        break;
                    case ModeOperType.Y7:
                        if (bussiness.BtY7 != 0)
                        {
                            this.Y7.BackColor = SetRunColorState(color);
                        }
                        else
                        {
                            this.Y7.BackColor = SetRunColorState(ColorType.Default);
                        }
                        break;
                    case ModeOperType.Y10:
                        if (bussiness.BtY10 != 0)
                        {
                            this.Y10.BackColor = SetRunColorState(color);
                        }
                        else
                        {
                            this.Y10.BackColor = SetRunColorState(ColorType.Default);
                        }
                        break;
                    case ModeOperType.Y11:
                        if (bussiness.BtY11 != 0)
                        {
                            this.Y11.BackColor = SetRunColorState(color);
                        }
                        else
                        {
                            this.Y11.BackColor = SetRunColorState(ColorType.Default);
                        }
                        break;
                    case ModeOperType.Y12:
                        if (bussiness.BtY12 != 0)
                        {
                            this.Y12.BackColor = SetRunColorState(color);
                        }
                        else
                        {
                            this.Y12.BackColor = SetRunColorState(ColorType.Default);
                        }
                        break;
                    case ModeOperType.Y13:
                        if (bussiness.BtY13 != 0)
                        {
                            this.Y13.BackColor = SetRunColorState(color);
                        }
                        else
                        {
                            this.Y13.BackColor = SetRunColorState(ColorType.Default);
                        }
                        break;
                    case ModeOperType.Y14:
                        if (bussiness.BtY14 != 0)
                        {
                            this.Y14.BackColor = SetRunColorState(color);
                        }
                        else
                        {
                            this.Y14.BackColor = SetRunColorState(ColorType.Default);
                        }
                        break;
                    case ModeOperType.Y15:
                        if (bussiness.BtY15 != 0)
                        {
                            this.Y15.BackColor = SetRunColorState(color);
                        }
                        else
                        {
                            this.Y15.BackColor = SetRunColorState(ColorType.Default);
                        }
                        break;
                    case ModeOperType.Y16:
                        if (bussiness.BtY16 != 0)
                        {
                            this.Y16.BackColor = SetRunColorState(color);
                        }
                        else
                        {
                            this.Y16.BackColor = SetRunColorState(ColorType.Default);
                        }
                        break;
                    case ModeOperType.Y17:
                        if (bussiness.BtY17 != 0)
                        {
                            this.Y17.BackColor = SetRunColorState(color);
                        }
                        else
                        {
                            this.Y17.BackColor = SetRunColorState(ColorType.Default);
                        }
                        break;
                    case ModeOperType.Y20:
                        if (bussiness.BtY20 != 0)
                        {
                            this.Y20.BackColor = SetRunColorState(color);
                        }
                        else
                        {
                            this.Y20.BackColor = SetRunColorState(ColorType.Default);
                        }
                        break;
                    case ModeOperType.ResetAll:
                        if (bussiness.BtReset != 0)
                        {
                            this.Y0.BackColor = SetRunColorState(color);
                            this.Y1.BackColor = SetRunColorState(color);
                            this.Y2.BackColor = SetRunColorState(color);
                            this.Y3.BackColor = SetRunColorState(color);
                            this.Y4.BackColor = SetRunColorState(color);
                            this.Y5.BackColor = SetRunColorState(color);
                            this.Y6.BackColor = SetRunColorState(color);
                            this.Y7.BackColor = SetRunColorState(color);
                            this.Y10.BackColor = SetRunColorState(color);
                            this.Y11.BackColor = SetRunColorState(color);
                            this.Y12.BackColor = SetRunColorState(color);
                            this.Y13.BackColor = SetRunColorState(color);
                            this.Y14.BackColor = SetRunColorState(color);
                            this.Y15.BackColor = SetRunColorState(color);
                            this.Y16.BackColor = SetRunColorState(color);
                            this.Y17.BackColor = SetRunColorState(color);
                            this.Y20.BackColor = SetRunColorState(color);
                        }
                        else
                        {
                            this.Y0.BackColor = SetRunColorState(ColorType.Default);
                            this.Y1.BackColor = SetRunColorState(ColorType.Default);
                            this.Y2.BackColor = SetRunColorState(ColorType.Default);
                            this.Y3.BackColor = SetRunColorState(ColorType.Default);
                            this.Y4.BackColor = SetRunColorState(ColorType.Default);
                            this.Y5.BackColor = SetRunColorState(ColorType.Default);
                            this.Y6.BackColor = SetRunColorState(ColorType.Default);
                            this.Y7.BackColor = SetRunColorState(ColorType.Default);
                            this.Y10.BackColor = SetRunColorState(ColorType.Default);
                            this.Y11.BackColor = SetRunColorState(ColorType.Default);
                            this.Y12.BackColor = SetRunColorState(ColorType.Default);
                            this.Y13.BackColor = SetRunColorState(ColorType.Default);
                            this.Y14.BackColor = SetRunColorState(ColorType.Default);
                            this.Y15.BackColor = SetRunColorState(ColorType.Default);
                            this.Y16.BackColor = SetRunColorState(ColorType.Default);
                            this.Y17.BackColor = SetRunColorState(ColorType.Default);
                            this.Y20.BackColor = SetRunColorState(ColorType.Default);
                        }
                        break;
                }
            }));
        }

        private void SetShePingBtEnable(bool isEnabed)
        {
            SetChBtEnable(isEnabed);
            this.Invoke(new MethodInvoker(() =>
            {
                this.button18.Enabled = isEnabed;
                this.button19.Enabled = isEnabed;
                this.button20.Enabled = isEnabed;
                this.button21.Enabled = isEnabed;
                this.button22.Enabled = isEnabed;
                this.button23.Enabled = isEnabed;
                this.button24.Enabled = isEnabed;
                this.button25.Enabled = isEnabed;
                this.button26.Enabled = isEnabed;
                this.button27.Enabled = isEnabed;
                this.button28.Enabled = isEnabed;
                this.button29.Enabled = isEnabed;
                this.button30.Enabled = isEnabed;
                this.button31.Enabled = isEnabed;
                this.button32.Enabled = isEnabed;
                this.button33.Enabled = isEnabed;
                this.button34.Enabled = isEnabed;
            }));
        }

        #endregion

        #region 日志
        public void Debug(string content)
        {
            Console.WriteLine(content);
        }

        public void Error(string content)
        {
            Console.WriteLine(content);
        }

        public void Error(Exception ex)
        {
            Console.WriteLine(string.Format("{0}\r\n{1}", ex.Message, ex.StackTrace));
        }

        public void Info(string content)
        {
            Console.WriteLine(content);
        }

        public void Write(string log)
        {
            Console.WriteLine(log);
        }

        #endregion

        /// <summary>
        /// 重绘按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Paint(object sender, PaintEventArgs e)
        {
            Button bt = sender as Button;
            GraphicsPath mygren = new GraphicsPath();
            System.Drawing.Rectangle rang = bt.ClientRectangle;
            rang.Inflate(-2, -1);
            e.Graphics.DrawEllipse(System.Drawing.Pens.BlanchedAlmond, rang);
            rang.Inflate(-2, -4);
            mygren.AddEllipse(rang);
            bt.Region = new Region(mygren);
        }

        /// <summary>
        /// 颜色定义
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        private Color SetRunColorState(ColorType state)
        {
            Color color = Color.Transparent;
            switch (state)
            {
                case ColorType.Default:
                    color = Color.DarkGray;
                    break;
                case ColorType.Error:
                    color = Color.Red;
                    break;
                case ColorType.Success:
                    color = Color.Green;
                    break;
            }
            return color;
        }

        /// <summary>
        /// 设置通道默认颜色
        /// </summary>
        public void SetCHDefaultState()
        {
            Ch1.BackColor = Color.DarkGray;
            Ch2.BackColor = Color.DarkGray;
            Ch3.BackColor = Color.DarkGray;
            Ch4.BackColor = Color.DarkGray;
            Ch5.BackColor = Color.DarkGray;
            Ch6.BackColor = Color.DarkGray;
            Ch7.BackColor = Color.DarkGray;
            Ch8.BackColor = Color.DarkGray;
            Ch9.BackColor = Color.DarkGray;
            Ch10.BackColor = Color.DarkGray;
            Ch11.BackColor = Color.DarkGray;
            Ch12.BackColor = Color.DarkGray;
        }

        /// <summary>
        /// 射频通道默认颜色
        /// </summary>
        public void SetShePingDefaultState()
        {
            Y0.BackColor = Color.DarkGray;
            Y1.BackColor = Color.DarkGray;
            Y2.BackColor = Color.DarkGray;
            Y3.BackColor = Color.DarkGray;
            Y4.BackColor = Color.DarkGray;
            Y5.BackColor = Color.DarkGray;
            Y6.BackColor = Color.DarkGray;
            Y7.BackColor = Color.DarkGray;
            Y10.BackColor = Color.DarkGray;
            Y11.BackColor = Color.DarkGray;
            Y12.BackColor = Color.DarkGray;
            Y13.BackColor = Color.DarkGray;
            Y14.BackColor = Color.DarkGray;
            Y15.BackColor = Color.DarkGray;
            Y16.BackColor = Color.DarkGray;
            Y17.BackColor = Color.DarkGray;
            Y20.BackColor = Color.DarkGray;
        }
    }
}
